const conf = require('../conf/conf.json');

const diagonalLength = Math.hypot(300, 232)

const getModelType = (model) => {
    return model['$type'] ? model['$type'].split('.').pop().split(',')[0] : '';
}

const cleanObject = (obj) => {
    if (!conf.cleanJSON) {
        return obj
    }
    if (typeof obj !== 'object' || obj === null) {
        return typeof obj === 'number' && (obj === Infinity || isNaN(obj)) ? null : obj;
    }
    if (Array.isArray(obj)) {
        for (let i = obj.length - 1; i >= 0; i--) {
            const cleanedItem = cleanObject(obj[i]);
            if (typeof cleanedItem === 'undefined' ||
                cleanedItem === null ||
                (Array.isArray(cleanedItem) && cleanedItem.length === 0)) {
                obj.splice(i, 1);
            } else {
                obj[i] = cleanedItem;
            }
        }
        return obj;
    }
    for (const key in obj) {
        if (obj.hasOwnProperty(key)) {
            const cleanedValue = cleanObject(obj[key]);
            if (cleanedValue === undefined || cleanedValue === null ||
                (Array.isArray(cleanedValue) && cleanedValue.length === 0) ||
                (typeof cleanedValue === 'object' && Object.keys(cleanedValue).length === 0)) {
                delete obj[key];
            } else {
                obj[key] = cleanedValue;
            }
        }
    }
    return obj;
}

const printDebugMsg = (...a) => {
    if (conf.debug) console.log(...a)
}

module.exports = {
    diagonalLength,
    getModelType,
    cleanObject,
    printDebugMsg
}